package com.taokatao.cardmee.common.widget;

import android.app.Activity;
import android.content.Context;
import android.content.res.TypedArray;
import android.os.Build;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.taokatao.cardmee.R;
import com.taokatao.cardmee.Utils;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;


public class TitleLayout extends LinearLayout implements View.OnClickListener {
    @BindView(R.id.v_fit) View vFit;
    @BindView(R.id.l_content) public View lContent;
    @BindView(R.id.i_back) ImageView iBack;
    @BindView(R.id.t_title) TextView tTitle;
    @BindView(R.id.t_menu) TextView tMenu;
    @BindView(R.id.i_menu) ImageView iMenu;
    @BindView(R.id.v_underline) View vUnderline;

    private Callback callback;
    private Callback2 callback2;

    boolean isDarkBackgroundColor;
    boolean isFitViewInnerControl = true;

    boolean isMyCustomLeftIcon = false;

    public TitleLayout(Context context) {
        this(context, null);
    }

    public TitleLayout(Context context, AttributeSet attrs) {
        super(context, attrs);
        ButterKnife.bind(this, LayoutInflater.from(context).inflate(R.layout.layout_title, this));

        TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.TitleLayout);
        hasUnderline(a.getBoolean(R.styleable.TitleLayout_hasUnderline, true));
        setTitleText(a.getString(R.styleable.TitleLayout_titleText));
        setBackIcon(a.getResourceId(R.styleable.TitleLayout_backIcon, 0));
        setMenuIcon(a.getResourceId(R.styleable.TitleLayout_menuIcon, 0));
        setMenuText(a.getString(R.styleable.TitleLayout_menuText));
        setBackgroundColor(a.getColor(R.styleable.TitleLayout_contentBackground, 0xFFFFFFFF));
        a.recycle();
    }

    @Override
    @OnClick({R.id.i_back, R.id.i_menu, R.id.t_menu})
    public void onClick(View v) {
        if (v == iBack) {
            if (callback2 == null) {
                ((Activity) getContext()).onBackPressed();
            } else {
                callback2.onBackClick(this);
            }
        } else if (v == iMenu) {
            if (callback != null) callback.onMenuClick(this, "image");
        } else if (v == tMenu) {
            if (callback != null) callback.onMenuClick(this, "text");
        }
    }

    @Override
    public void setBackgroundColor(int color) {
        lContent.setBackgroundColor(color);
        switchContentStyleByColor(color);
    }

    public void switchContentStyleByColor(int color) {
        isDarkBackgroundColor = Utils.isDarkColorTheme(color);
        tTitle.setTextColor(isDarkBackgroundColor ? 0xFFFFFFFF : 0xFF333333);
        tMenu.setTextColor(isDarkBackgroundColor ? 0xFFFFFFFF : 0xFF333333);

        if (!isMyCustomLeftIcon) {
            iBack.setImageResource(isDarkBackgroundColor ? R.mipmap.common_back_white : R.mipmap.common_back_black);
        }

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            iBack.setBackgroundResource(isDarkBackgroundColor ? R.drawable.ripple_back_white : R.drawable.ripple_back_black);
            iMenu.setBackgroundResource(isDarkBackgroundColor ? R.drawable.ripple_back_white : R.drawable.ripple_back_black);
            tMenu.setBackgroundResource(isDarkBackgroundColor ? R.drawable.ripple_back_white : R.drawable.ripple_back_black);
        }

        if (!isInEditMode() && isFitViewInnerControl) {
            Utils.fitSysWindowAndSetStatusBarMode(color, vFit);
        }
    }

    public void hasUnderline(boolean hasUnderline) {
        vUnderline.setVisibility(hasUnderline ? View.VISIBLE : View.GONE);
    }

    public void setTitleText(String titleText) {
        if (!TextUtils.isEmpty(titleText)) {
            if (titleText.length() > 10) {
                tTitle.setText(titleText.substring(10) + "..");
            } else {
                tTitle.setText(titleText);
            }
        } else {
            tTitle.setText("");
        }
    }

    public void setBackIcon(int backIcon) {
        if (backIcon != 0) {
            if (backIcon != R.mipmap.common_back_white && backIcon != R.mipmap.common_back_black) {
                isMyCustomLeftIcon = true;
            }
            iBack.setImageResource(backIcon);
            iBack.setVisibility(View.VISIBLE);
        } else {
            iBack.setVisibility(View.GONE);
        }
    }

    public void setMenuIcon(int menuIcon) {
        if (menuIcon != 0) {
            iMenu.setImageResource(menuIcon);
            iMenu.setVisibility(View.VISIBLE);
        } else {
            iMenu.setVisibility(View.GONE);
        }
    }

    public void setMenuText(String menuText) {
        if (!TextUtils.isEmpty(menuText)) {
            tMenu.setText(menuText);
            tMenu.setVisibility(View.VISIBLE);
        } else {
            tMenu.setVisibility(View.GONE);
        }
    }

    public View getFitView() {
        isFitViewInnerControl = false;
        return vFit;
    }

    public boolean isDarkBackgroundColor() {
        return isDarkBackgroundColor;
    }

    public void setCallback(Callback cb) {
        callback = cb;
    }

    public void setCallback2(Callback2 cb2) {
        callback2 = cb2;
    }

    public interface Callback {
        void onMenuClick(TitleLayout lTitle, String flag);
    }

    public interface Callback2 {
        void onBackClick(TitleLayout lTitle);
    }
}
